# Contributor: Anjandev Momi <anjan@momi.ca>
# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=nlopt
pkgver=2.8.0
pkgrel=0
pkgdesc="Library for nonlinear optimization"
url="https://github.com/stevengj/nlopt"
arch="all"
license="LGPL-2.1-or-later"
makedepends="
	cmake
	guile-dev
	python3-dev
	samurai
	swig
	"
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-guile
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/stevengj/nlopt/archive/refs/tags/v$pkgver.tar.gz"

case "$CARCH" in
# octave unavailable on these 3 archs
s390x|riscv64|ppc64le) ;;
*)
	makedepends="$makedepends octave-dev"
	subpackages="$subpackages $pkgname-octave"
	;;
esac

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		local crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=ON \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		$crossopts
	cmake --build build
}

check() {
	ctest --test-dir build --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

guile() {
	pkgdesc="$pkgdesc (Guile bindings)"
	depends="$pkgname=$pkgver-r$pkgrel guile"

	amove usr/lib/guile usr/share/guile
}

octave() {
	pkgdesc="$pkgdesc (Octave bindings)"
	depends="$pkgname=$pkgver-r$pkgrel octave"

	amove usr/lib/octave usr/share/octave
}

sha512sums="
cb294caa5532e11ae0d22ed849705920bbae79f712144c840a5ca865ef8e6a15c6c9540c81ced0c3c05b9f44c360d50f74e235e69d893be34b7e1c5599f07c71  nlopt-2.8.0.tar.gz
"
